<?php

namespace Home\Controller;

use Think\Controller;

/** 
 * @author Administrator
 * 
 */
class SwooleController extends Controller {
    
    public function laychat(){
        $this->display();
    }
    
    public function laychat_login(){
        //保存信息
        echo '<script type="text/javascript" src="http://qzonestyle.gtimg.cn/qzone/openapi/qc_loader.js" charset="utf-8" data-callback="true"></script>';exit;
    }
    
    public function checkLogin(){
        $userid = cookie('userid');
        //没有相关session则跳转到登录页
        if(!session('userid') || $userid != session('userid')){
            $this->error('请登陆', U('Swoole/login'));
            redirect(U('Swoole/login'), 1);
        }
    }

    //聊天室
    public function chat(){
        $this->checkLogin();
        $this->display();
    }
    
    //聊天室
    public function layim(){
        $this->checkLogin();
        
        $this->assign('username', cookie('username'));
        $this->assign('userid', cookie('userid'));
        $this->display();
    }

    //查找好友页面
    public function layimFind(){

        $this->display();
    }

    //获取好友列表
    public function ajax_friendList(){
        $page = max(1, I('get.page', '1', 'intval'));
        $pagesize = 12;
        //获取好友
        $userid = $ids = session('userid');
        $friend_list = M('UserFriend')->where(array('userid' => $userid))->select();
        if($friend_list){
            foreach ($friend_list as $value){
                $ids .= ','.$value['friendid'];
            }
        }
        $map['userid'] = array('notin', $ids);
        $result = M('User')->where($map)->field('userid, username')->page($page, $pagesize)->select();

        $user_list = array();
        if($result){
            foreach ($result as $key => $value){
                $user_list[$key]['Id'] = $value['friendid'];
                $user_list[$key]['username'] = $value['username'];
                $user_list[$key]['remark'] = 'demo';
                $user_list[$key]['avatar'] = 'demo.jpg';
            }
        }
        $this->ajaxReturn($user_list);
    }

    //获取好友分组
    public function ajax_getFriendGroup(){
        echo '<option value="1">我的好友</option>';
        exit;
    }
    
    //获取界面主信息
    public function ajax_getList(){
        $userid = I('get.userid', 0, 'intval');
        $userid = $userid ? $userid : session('userid');
        
        //$this->checkLogin();
        $userinfo = M('User')->where(array('userid' => $userid))->find();
        $userinfo['avatar'] = $userinfo['avatar'] ? $userinfo['avatar'] : 'http://tp1.sinaimg.cn/1571889140/180/40030060651/1';
        $mine = array(
            'username'  => $userinfo['nickname'],
            'userid'    => $userinfo['userid'],
        );
        $friend = array(
            'groupname' => '码屌',
            'id'        => 1,
            'online'    => 1,
            'list'      => array()
        );
        $return['code']     = 0;
        $return['msg']      = '';
        $return['mine']     = $mine;
        $return['friend']   = $friend;
        //$this->ajaxReturn($return, '', JSON_FORCE_OBJECT);
         echo '
         {
  "code": 0
  ,"msg": ""
  ,"data": {
    "mine": {
      "username": "'.$userinfo['nickname'].'"
      ,"id": '.$userid.'
      ,"status": "online"
      ,"sign": "在深邃的编码世界，做一枚轻盈的纸飞机"
      ,"avatar": "'.$userinfo['avatar'].'"
    }
    ,"friend": [{
      "groupname": "我的好友"
      ,"id": 1
      ,"online": 2
      ,"list": []
    },{
      "groupname": "在线好友"
      ,"id": 2
      ,"online": 3
      ,"list": [{
        "username": "晴儿"
        ,"id": "1"
        ,"avatar": "http://tp1.sinaimg.cn/1571889140/180/40030060651/1"
        ,"sign": "没人在线？我陪你聊"
      }]
    }]
  }
}';
    }
    
    public function login(){
        if(IS_POST){
            $ref_url = I('post.ref_url') ? I('post.ref_url') : U('Swoole/layim');
            
            $map['username'] = I('post.username');
            $map['password'] = I('post.password');
            if(!$map['username'] || !$map['password']){
                $this->error('账号密码错误');
            }
            $info = M('User')->where($map)->find();
            if($info){
                cookie('username', $map['username']);
                cookie('userid', $info['userid']);
                session('userid', $info['userid']);
                $this->error('登陆成功', $ref_url);
                //redirect(U('Swoole/layim'));
            }else{
                $this->error('账号密码错误');
            }
        }else{
            $ref_url = $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : '';
            $this->assign('ref_url', $ref_url);
            $this->display();
        }
    }
    
    
    public function register(){
        if(IS_POST){
            $info['username'] = $info['nickname'] = I('post.username');
            $info['password'] = I('post.password');

            if(!$info['username'] || !$info['password']){
                $this->error('账号密码不能为空');
            }
            //用户名不重复
            $exist_name = M('User')->where(array('username' => $info['username']))->find();
            if($exist_name){
                $this->error('该用户名已存在');
            }
            $userid = M('User')->add($info);
            //直接登陆
            cookie('userid', $userid);
            cookie('username', $info['username']);
            session('userid', $userid);
            redirect(U('Swoole/layim'));
        }else{
            $this->display();
        }
    }
    
    public function logout(){
        session(null); 	// 清空当前的session
        cookie(null);
        redirect(U('Swoole/login'));
    }
}

